package com.yanghang.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yanghang.entity.Account;
import com.yanghang.service.AccountService;
import com.yanghang.util.JedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.Map;

@RestController
public class AccountController {
    @Autowired
    private AccountService as;
    @GetMapping("/account")
    public Account ifLogin(String cardno, String password){
        Account account = as.login(cardno, password);
        if(account!=null){
            try {
                ObjectMapper mapper=new ObjectMapper();
                String s = mapper.writeValueAsString(account);
                Jedis jedis = JedisUtils.getJedis();
                jedis.set(String.valueOf(account.getCardno()),s);
            } catch (JsonProcessingException e) {
              throw new RuntimeException(e);
            }
        }
        return account;
    }
    @GetMapping("/exit/account/{cardno}")
    public void exitLogin(@PathVariable("cardno") String cardno){
        Jedis jedis = JedisUtils.getJedis();
        jedis.del(cardno);
    }

    @GetMapping("/check/account/{cardno}")
    public String checkAccount(@PathVariable("cardno")String cardno){
        Account account = as.selectAccountByCardNo(cardno);
        if(account==null){
            return "-1";
        }else if(account.getStatus()==0){
            return"0";
        }else{
            return"1";
        }
    }

    @GetMapping("/transfor/account")
    public void transforAccount(String sendCardNo, String receiveCardNo, Double price ){
        as.transforCount(sendCardNo, receiveCardNo, price);
    }
}
